home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wpjv1n1.zip / LINKLIST.C next >
Text File  |  1993-01-01  |  6KB  |  217 lines

  1. /*  LINKLIST.C
  2.     by Mike Wallace
  3.     Windows Programmer's Journal
  4.     Volume 1 Number 1
  5.     Copyright 1992
  6. */
  7.  
  8. /* linked list windows application */
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include "linklist.h"
  12.  
  13. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  14.                     LPSTR lpszCmdLine, int nCmdShow)
  15. {
  16.  
  17.         HWND hWnd;
  18.         MSG msg;
  19.         WNDCLASS wndclass;
  20.  
  21.         ghInstance = hInstance;
  22.  
  23.         if(!hPrevInstance)
  24.         {
  25.                 wndclass.style = CS_HREDRAW | CS_VREDRAW;
  26.                 wndclass.lpfnWndProc = WndProc;
  27.                 wndclass.cbClsExtra = 0;
  28.                 wndclass.cbWndExtra = 0;
  29.                 wndclass.hInstance = hInstance;
  30.                 wndclass.hIcon = LoadIcon (hInstance, ProgName);
  31.                 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  32.                 wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  33.                 wndclass.lpszMenuName = ProgName;
  34.                 wndclass.lpszClassName = ProgName;
  35.  
  36.                 if (!RegisterClass (&wndclass)) return FALSE;
  37.  
  38.         }
  39.  
  40.         hWnd = CreateWindow (
  41.                 ProgName,
  42.                 ProgName,
  43.                 WS_OVERLAPPEDWINDOW,
  44.                 CW_USEDEFAULT,
  45.                 CW_USEDEFAULT,
  46.                 CW_USEDEFAULT,
  47.                 CW_USEDEFAULT,
  48.                 NULL,
  49.                 NULL,
  50.                 hInstance,
  51.                 NULL);
  52.  
  53.         ShowWindow (hWnd, nCmdShow);
  54.         UpdateWindow (hWnd);
  55.  
  56.         while (GetMessage (&msg, NULL, 0, 0))
  57.         {
  58.                 TranslateMessage (&msg);
  59.                 DispatchMessage (&msg);
  60.         }
  61.         return msg.wParam;
  62. } /* WinMain */
  63.  
  64. long FAR PASCAL WndProc (HWND hWnd, unsigned Msg, WORD wParam, LONG lParam)
  65. {
  66.  
  67.         switch (Msg)
  68.         {
  69.                 case WM_COMMAND:
  70.                         switch (wParam)
  71.                         {
  72.                         case IDM_RUN:
  73.                                 MakeList (hWnd);
  74.                                            MessageBox (hWnd, "Done", "Hey", MB_OK);  
  75.                                 break;
  76.                         
  77.                         case IDM_QUIT:
  78.                                 DestroyWindow (hWnd);
  79.                                 break;
  80.                         }
  81.                         break;
  82.                 case WM_DESTROY:
  83.                         PostQuitMessage (0);
  84.                         break;
  85.                 default:
  86.                         return DefWindowProc (hWnd, Msg, wParam, lParam);
  87.         }
  88.         return (0L);
  89. } /* WndProc */
  90.  
  91. /****************************************/
  92. /*                                      */
  93. /*                                      */
  94. /****************************************/
  95.  
  96. void FAR PASCAL MakeList (HWND hWnd)
  97. {
  98.     /* local variables */
  99.     short           i;
  100.     static HANDLE   hMem, hMem2, hMem3;
  101.  
  102.     /* structure of each node in linked list */
  103.     typedef struct _listmarker {
  104.  
  105.         short   value;
  106.         HANDLE  next;
  107.  
  108.     } ListMarker;
  109.  
  110.     /* declare a far pointer to the above structure */
  111.     typedef ListMarker FAR *LISTMARKER;
  112.  
  113.     LISTMARKER tailPos, tempPos, headPos;
  114.  
  115.     /* pointer to output file */
  116.     FILE    *fp;
  117.  
  118.     /* Open output file for writing */
  119.     fp= fopen("data.out", "w");
  120.  
  121.     /* Build initial linked list of the numbers 1 to 10 */
  122.     if((hMem= GlobalAlloc(GMEM_DISCARDABLE | GMEM_MOVEABLE, 
  123.         sizeof(ListMarker))) == NULL) {
  124.  
  125.         /* Not enough memory, so beep, show a message and return */
  126.         MessageBeep(0);
  127.         MessageBox(hWnd, "Out of allocation memory!", "ERROR", MB_OK);
  128.  
  129.         return;
  130.             
  131.     }
  132.  
  133.     /* Lock the first node, save a value, set next to NULL and unlock */
  134.     tailPos= headPos= (LISTMARKER) GlobalLock(hMem);
  135.     headPos->value= 1;
  136.     headPos->next= NULL;
  137.     GlobalUnlock(hMem);
  138.     
  139.     for (i=2; i < 11; i++) {
  140.             
  141.         /* setup index lookup lists */
  142.         if((tailPos->next= GlobalAlloc(GMEM_DISCARDABLE | GMEM_MOVEABLE, 
  143.             sizeof(ListMarker))) == NULL) {
  144.     
  145.             MessageBeep(0);
  146.             MessageBox(hWnd, "Out of allocation memory!", "ERROR",
  147.                             MB_OK);
  148.  
  149.             return;
  150.                     
  151.         }  /* If - End */
  152.  
  153.         /* Lock the next node, save the value, and set its next to NULL */
  154.         hMem2= tailPos->next;
  155.         tempPos= (LISTMARKER) GlobalLock(hMem2);
  156.         tailPos= tempPos;
  157.         tailPos->value= i;
  158.         tailPos->next= NULL;
  159.  
  160.         GlobalUnlock(hMem2);
  161.                     
  162.     }  /* While - End */
  163.  
  164.     /* Lock the 1st node and write out its "value" field */
  165.     tailPos= headPos= (LISTMARKER) GlobalLock(hMem);
  166.     fprintf(fp, "%d\n", tailPos->value);
  167.  
  168.     /* Save the handle to the next node */
  169.     hMem2= tailPos->next;
  170.  
  171.     /* Unlock the 1st node */
  172.     GlobalUnlock(hMem);
  173.  
  174.     /* Go through list and print out "value" until no more nodes */
  175.     while (hMem2 != NULL) {
  176.         
  177.         /* Lock the next node and save to tailPos */
  178.         tempPos= (LISTMARKER) GlobalLock(hMem2);
  179.         tailPos= tempPos;
  180.  
  181.         fprintf(fp, "%d\n", tailPos->value);
  182.  
  183.         /* Get the handle to the next node and then unlock the current one */
  184.         hMem2= tailPos->next;
  185.         GlobalUnlock(hMem2);
  186.                             
  187.     } /* While - End */
  188.  
  189.     /* Close the output file */
  190.     fclose(fp);                    
  191.  
  192.     /* free nodes in the list */
  193.     tempPos= (LISTMARKER) GlobalLock(hMem);
  194.     hMem2= tempPos->next;
  195.     tempPos= (LISTMARKER) GlobalLock(tempPos->next);
  196.     GlobalUnlock(hMem);
  197.     GlobalFree(hMem);
  198.  
  199.     while(tempPos->next != NULL) {
  200.  
  201.        hMem3= tempPos->next;
  202.        tempPos= (LISTMARKER) GlobalLock(tempPos->next);
  203.        GlobalUnlock(hMem2);
  204.        GlobalFree(hMem2);
  205.        hMem2=hMem3;
  206.  
  207.     }
  208.  
  209.     GlobalUnlock(hMem2);
  210.     GlobalFree(hMem2);
  211.  
  212.     return;
  213.  
  214. } /* MakeList */
  215.  
  216. 
  217.